<?php
	/**
	*模型字段管理类
	**/
	class FieldClass {
		// 构造函数
		public function __constract(){
		}

		// 获取模型字段列表
		public function getList($data){
			global $db;
			$modelId = $data->modelId;

			// 获取所有数据
			$db->query("select
							field.id,
							field.name,
							field.field,
							field.formtype as formType,
							field.val,
							field.comment,
							field.create_time as createTime,
							user.name as createName
						from model_field as field
						left join admin_user as user
						on field.creator=user.id
						where modelid=:modelId",
						array(
							"modelId"=>$modelId
							));

			return $db->prepare->fetchAll();
		}

		// 添加模型字段
		public function add($data){
			global $db, $session;
			$loginUserId = $session->get("userId");
			$name = $data->name;
			$modelId = $data->modelId;
			$field = $data->field;
			$val = $data->val;
			$comment = $data->comment;
			$formType = $data->formType;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 查询模型表名称和模型id是否已经存在
			$db->query("select id 
						from model_field
						where
							field=:field and
							modelid=:modelId
						for update",
						array(
							"field"=>$field,
							"modelId"=>$modelId
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "字段表名称和模型id已经存在了，请重新对表字段名称命名！", null);
			}

			// 插入一条新数据
			$db->query("insert into model_field(
							name,
							modelid,
							field,
							formtype,
							val,
							comment,
							creator) 
						values(
							:name,
							:modelid,
							:field,
							:formType,
							:val,
							:comment,
							:creator
						)", 
						array(
							"name"=>$name,
							"modelid"=>$modelId,
							"field"=>$field,
							"formType"=>$formType,
							"val"=>$val,
							"comment"=>$comment,
							"creator"=>$loginUserId,
							)
						);

			// 插入模型表字段
			$fieldArr = array(
				0=>array(
					"name"=>"text",
					"type"=>"varchar",
					"maxLength"=>255,
					"default"=>'#'),
				1=>array(
					"name"=>"select",
					"type"=>"tinyint",
					"maxLength"=>1,
					"default"=>0),
				2=>array(
					"name"=>"radio",
					"type"=>"tinyint",
					"maxLength"=>1,
					"default"=>0),
				3=>array(
					"name"=>"checkbox",
					"type"=>"varchar",
					"maxLength"=>255,
					"default"=>'0'),
				4=>array(
					"name"=>"text",
					"type"=>"varchar",
					"maxLength"=>500,
					"default"=>''),
				5=>array(
					"name"=>"file",
					"type"=>"varchar",
					"maxLength"=>100,
					"default"=>'')
				);
			// 获取模型对应的数据存储表名称
			$tableName = $this->getModelTotableName($modelId);
			$sql = "alter table " . $tableName . " add " . $field . " " . $fieldArr[$formType]["type"] . "(" . $fieldArr[$formType]["maxLength"]. ") not null default '" . $fieldArr[$formType]["default"] . "' comment '" . $comment . "'";
			$db->query($sql, "");

			$db->PDO->commit();
			// 事物结束

			// 删除session中modelToTableName数据
			$session->del('modelToTableName');
		}

		// 更新模型
		public function edit($data){
			global $db;
			$id = $data->id;
			$name = $data->name;
			$val = $data->val;
			$comment = $data->comment;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 判断id参数是否有误
			$db->query("select * 
						from model_field
						where 
							id=:id
						for update",
						array(
							"id"=>$id
							));
			$result = $db->prepare->fetchAll();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "参数有误！", null);
			}

			// 更新数据
			$db->query("update model_field
						set
							name=:name,
							val=:val,
							comment=:comment
						where
							id=:id",
						array(
							"id"=>$id,
							"name"=>$name,
							"val"=>$val,
							"comment"=>$comment
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}

			$db->PDO->commit();
			// 事物结束
		}

		// 删除模型字段
		public function del($data){
			global $db, $session;
			$id = $data->id;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 查询id参数是否有误
			$db->query("select modelid,field
						from model_field
						where
							id=:id",
						array(
							"id"=>$id
							));
			$result = $db->prepare->fetchAll();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "参数有误！", null);
			}
			$modelId = $result[0]["modelid"];
			$fieldName = $result[0]["field"];

			// 判断模型数据表里是否已经有数据了
			$tableName = $this->getModelTotableName($modelId);
			$sql = "select id from " . $tableName;
			$db->query($sql, "");
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "模型数据表已经有数据了，不能删除模型字段！", null);
			}

			// 删除modle_field表里对应id的数据
			$db->query("delete from model_field
						where 
							id=:id",
						array(
							"id"=>$id
							)
						);

			// 删除模型数据表里对应字段
			$sql = "alter table " . $tableName . " drop " . $fieldName;
			$db->query($sql, "");
			
			$db->PDO->commit();
			// 事物结束
			
			// 删除session中modelToTableName数据
			$session->del('modelToTableName');
		}

		private function getModelTotableName($modelId){
			global $db, $session;
			$modelTotableName = $session->get('modelToTableName');

			if($modelTotableName){
				$modelTotableName = unserialize($modelTotableName);
			} else {
				// 获取模型和数据表名称数据
				$db->query("select id as modelId, table_name as tableName from model", "");
				$result = $db->prepare->fetchAll();
				
				$len = count($result);
				for($i=0; $i<$len; $i++){
					$item = $result[$i];
					$modelTotableName[$item['modelId']] = $item['tableName'];
				}
				$session->set('modelToTableName', serialize($modelTotableName));
			}
			return $modelTotableName[$modelId];
		}
	}
?>